package com.nikanbujian.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nikanbujian.reggie.common.R;
import com.nikanbujian.reggie.entity.Category;
import com.nikanbujian.reggie.service.ICategoryService;
import com.nikanbujian.reggie.service.IEmployeeService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-08-19
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    ICategoryService iCategoryService;
    @PostMapping
    public R<String> save(@RequestBody Category category){
        iCategoryService.save(category);
        return R.success("新增分类成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize){
        Page page1=new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        iCategoryService.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }
    @DeleteMapping
    public R<String> delete(Long ids){
        log.info("idididid",ids);
        iCategoryService.removeById(ids);
    return R.success("删除成功");
    }

    @PutMapping
    public R<String> update(@RequestBody Category category){
        iCategoryService.updateById(category);
        return R.success("修改成功");
    }

    @GetMapping("/list")
    public R<List<Category>> list( Category category){
        LambdaQueryWrapper<Category> categoryLambdaQueryWrapper=new LambdaQueryWrapper<>();
        categoryLambdaQueryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        categoryLambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> list = iCategoryService.list(categoryLambdaQueryWrapper);
        return R.success(list);
    }

}
